import request from "@/utils/request";

interface SearchParams {
  keyword?: string;
  status?: number;
  startTime?: string;
  endTime?: string;
}

// 获取所有优惠券类型
export const getAllCoupons = (
  pageNo: number,
  pageSize: number,
  params?: SearchParams
) => {
  // 构建查询参数
  const queryParams = new URLSearchParams({
    pageNo: pageNo.toString(),
    pageSize: pageSize.toString()
  });

  // 添加可选的搜索参数
  if (params?.keyword) {
    queryParams.append("keyword", params.keyword);
  }
  if (params?.status !== undefined) {
    queryParams.append("status", params.status.toString());
  }
  if (params?.startTime) {
    queryParams.append("startTime", params.startTime);
  }
  if (params?.endTime) {
    queryParams.append("endTime", params.endTime);
  }

  return request({
    url: `/coupons/getAllCoupons?${queryParams.toString()}`,
    method: "get"
  });
};

// 增加优惠券
export const addCoupons = (data: any, count: number) => {
  return request({
    url: `/coupons/addCoupons/${count}`,
    method: "post",
    data
  });
};

// 批量删除优惠券
export const deleteCoupons = (ids: number[]) => {
  return request({
    url: `/coupons`,
    method: "delete",
    data: ids
  });
};

// 删除单个优惠券
export const deleteCoupon = (id: number) => {
  return request({
    url: `/coupons/${id}`,
    method: "delete"
  });
};

// 修改优惠券
export const updateCoupon = (id: number, data: any) => {
  return request({
    url: `/coupons/${id}`,
    method: "put",
    data
  });
};
